#![cfg_attr(
  all(not(debug_assertions), target_os = "windows"),
  windows_subsystem = "windows"
)]

mod handle;
mod hack;
mod http;

use std::{path::PathBuf, sync::Arc};

// use tauri::Manager;
// use tauri::Manager;
use tauri::api::path;

use fill_core::{Node, api::Ctx};
use crate::handle::{encrypt_file, decrypt_file, decrypt_content, encrypt_content, unzip, zip, upload_file, reqwest_multipart_form};
use crate::hack::show_in_folder;
use crate::http::init;
use crate::http::{rsync_list};

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command

#[tokio::main]
async fn main() {
  let data_dir = path::data_dir()
    .unwrap_or_else(|| PathBuf::from("/"))
    .join("fill-turbo");

  println!("数据目录 - {}", data_dir.to_str().unwrap());

  let (client, router) = Node::new(data_dir).await.unwrap();

  tauri::Builder::default()
    // .setup(|app| {
    //     // #[cfg(debug_assertions)] // only include this code on debug builds
    //     {
    //         let window = app.get_window("main").unwrap();
    //         window.open_devtools();
    //         // window.close_devtools();
    //     }
    //     Ok(())
    // })
    .plugin(rspc::integrations::tauri::plugin(router, move || {
      Ctx {
        client: Arc::clone(&client)
      }
    }))
    .plugin(init())
    .invoke_handler(tauri::generate_handler![
        zip, unzip, encrypt_content, decrypt_content, encrypt_file, decrypt_file, show_in_folder, rsync_list, upload_file, reqwest_multipart_form
    ])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
